package cn.myweixin.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DownLoadServlet
 * 生产动态url，提供下载服务
 */
@WebServlet("/DownLoadServlet")
public class DownLoadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		  doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//得到要下载的文件名
		request.setCharacterEncoding("utf-8");
        String fileName = request.getParameter("filename");
   
        //上传转化后的文件都是保存在/WEB-INF/pdf目录下的子目录当中
        String fileSaveRootPath=this.getServletContext().getRealPath("/");
        //        处理文件名
         String realname = fileName.substring(fileName.lastIndexOf(File.separator)+1);
        //通过文件名找出文件的所在目录
       // String path = findFileSavePathByFileName(fileName,fileSaveRootPath);
        //得到要下载的文件
         
      File file = new File(fileSaveRootPath+fileName);
         //File file = new File(fileName);
        //如果文件不存在
        if(!file.exists()){
            request.setAttribute("message", "您要下载的资源已被删除！！");
            request.getRequestDispatcher("/message.jsp").forward(request, response);
            return;
        }
        
         //设置响应头，控制浏览器下载该文件
         response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
         //读取要下载的文件，保存到文件输入流
         FileInputStream in = new FileInputStream(fileSaveRootPath+fileName);
         //创建输出流
         OutputStream os = response.getOutputStream();
         //设置缓存区
         byte[] bytes = new byte[1024];
         int len = 0;
         while((len = in.read(bytes))>0){
             os.write(bytes);
         }
         //关闭输入流
         in.close();
         //关闭输出流
         os.close();
	}
	
	 //通过文件名和存储上传文件根目录找出要下载的文件的所在路径
    public String findFileSavePathByFileName(String fileName,String fileSaveRootPath){
        int hashcode = fileName.hashCode();
        int dir1 = hashcode&0xf;
        int dir2 = (hashcode&0xf0)>>4;
        String dir = fileSaveRootPath + "\\" + dir1 + "\\" + dir2;
        File file = new File(dir);
        if(!file.exists()){
            file.mkdirs();
        }
        return dir;
    }

}
